﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core.Base;
using System.ComponentModel;
using Base;
using System.Drawing;
using Core.Component;

namespace Components
{
    public class SGVNumericUpDown: NumericUpDown, IBindingControl, ISGVControl
    {
        public SGVNumericUpDown()
            :base()
        {
            Controls[0].Visible = false;
            this.CausesValidation = false;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.GotFocus += new EventHandler(SGVComboBox_GotFocus);
            this.LostFocus += new EventHandler(SGVComboBox_LostFocus);

            this.KeyUp += SGVTextBox_KeyUp;

        }

        void SGVTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(this.Text))
            {
                try
                {
                    System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("vi-VN");
                    float valueBefore = float.Parse(this.Text, System.Globalization.NumberStyles.AllowThousands);
                    this.Text = String.Format(culture, "{0:0,0.00}", valueBefore);
                    this.Select(this.Text.Length, 0);
                }
                catch (Exception ex)
                {
                    
                }
               
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
           
            if (keyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        void SGVComboBox_LostFocus(object sender, EventArgs e)
        {
            SGVControlHelper.ControlLostFocus(this);
        }

        void SGVComboBox_GotFocus(object sender, EventArgs e)
        {
            SGVControlHelper.ControlFocus(this);
        }


        #region IBindingControl
        BaseModule module;
        BaseEntity entity;
        String _bindingPropertyName = "Text";

        String _bindingTable;
        String _bindingColumn;
        String _dataSourceTable;
        String _dataSourceDisplayColumn;
        String _dataSourceKeyColumn;
        List<String> _dataSourceColumns;

        String _sGVTag;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get
            {
                return _sGVTag;
            }
            set
            {
                _sGVTag = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        string IBindingControl.BindingPropertyName
        {
            get
            {
                return _bindingPropertyName;
            }
            set
            {
                _bindingPropertyName = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingTable
        {
            get
            {
                return _bindingTable;
            }
            set
            {
                _bindingTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingColumn
        {
            get
            {
                return _bindingColumn;
            }
            set
            {
                _bindingColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceTable
        {
            get
            {
                return _dataSourceTable;
            }
            set
            {
                _dataSourceTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceDisplayColumn
        {
            get
            {
                return _dataSourceDisplayColumn;
            }
            set
            {
                _dataSourceDisplayColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceKeyColumn
        {
            get
            {
                return _dataSourceKeyColumn;
            }
            set
            {
                _dataSourceKeyColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public List<string> DataSourceColumns
        {
            get
            {
                return _dataSourceColumns;
            }
            set
            {
                _dataSourceColumns = value;
            }
        }

        public void RegisterModule(Core.Base.BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        #endregion


        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    int num1;
        //    Size size1;
        //    size1 = base.ClientSize;
        //    num1 = (size1.Height / 2);
        //    ControlPaint.DrawScrollButton(e.Graphics, new Rectangle(0, 0, 16, num1), 0, ((this.pushed == 1) ? 512 : 0));
        //    ControlPaint.DrawScrollButton(e.Graphics, new Rectangle(0, num1, 16,num1), 1, ((this.pushed == 2) ? 512 : 0));
        //    base.OnPaint(e);
        //}






        public void Init()
        {
           
        }


        public void UpdateFormat()
        {
        }
    }
}
